﻿declare var Xrm: Xrm;
declare var ExecutionContextObj: ExecutionContext;
declare var GetGlobalContext: GetGlobalContext;

interface Xrm {
    Page: Page;
    Utility: Utility;
}

interface ExecutionContext {
    /**
     * Method that returns the Client-side context (client-side reference) object.
     * @returns {Context} 
     */
    getContext(): Context;

    /**
     * Method that returns a value that indicates the order in which this handler is executed.
     * @returns {number} 
     */
    getDepth(): number;

    /**
     * Method that returns an object with methods to manage the Save event.
     * NOTE: This method returns null for any event other than the Save event.
     * @returns {} 
     */
    getEventArgs(): EventArgs;

    /**
     * Method that returns a reference to the object that the event occurred on.
     * @returns {} 
     */
    getEventSource(): Attribute;

    /**
     * Method that returns a reference to either the form (Xrm.Page) or editable grid depending on where the method was called.
     * @returns {Page|Object} 
     */
    getFormContext(): Page | Object;

    /**
     * Sets the value of a variable to be used by a handler after the current handler completes.
     * @param key The name of the variable
     * @param value The value to set.
     * @returns {} 
     */
    setSharedVariable(key: string, value: any): void;

    /**
     * Retrieves a variable set using setSharedVariable.
     * @param key The name of the variable.
     * @returns {any} The specific type depends on what the value object is.
     */
    getSharedVariable(key: string): any;
}

interface GetGlobalContext {
    (): Context;
}

interface Page {
    /**
     * Provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string.
     */
    context: Context;

    /**
     * provides methods to work with the form. You can refresh the data in the form and save the form asynchronously.
     */
    data: Data;

    /**
     * contains properties and methods to retrieve information about the user interface as well as collections for several subcomponents of the form.
     */
    ui: UI;

    /**
     * Access all attributes
     * @returns {Array<Attribute>} 
     */
    getAttribute(): Collection<Attribute>;

    /**
     * Access an attribute by name
     * @param name Name of the Attribute to get
     * @returns {Attribute} 
     */
    getAttribute(name: string): Attribute;

    /**
     * Access an attribute by index
     * @param index 
     * @returns {number} 
     */
    getAttribute(index: number): Attribute;

    /**
     * Access all attributes that meet a specific criteria
     * @param argument 
     * @returns {} 
     */
    getAttribute(argument: AttributeControlCallback<Attribute>): Collection<Attribute>;

    /**
     * Access all controls
     * @returns {Array<Control>}
     */
    getControl(): Collection<Control>;

    /**
     * Access a control by name
     * @param name Name of the control to get
     * @returns {Control}
     */
    getControl(name: string): Control;

    /**
     * Access an control by index
     * @param index 
     * @returns {number} 
     */
    getControl(index: number): Control;

    /**
     * Access all controls that meet a specific criteria
     * @param argument 
     * @returns {} 
     */
    getControl(argument: AttributeControlCallback<Control>): Collection<Control>;
}

interface Context {
    /**
     * Returns the base URL that was used to access the application.
     * @returns {string}
     */
    getClientUrl(): string;

    /**
     * Returns a string representing the current Microsoft Office Outlook theme chosen by the user.
     * @returns {string} 
     */
    getCurrentTheme(): string;

    /**
     * Returns whether Autosave is enabled for the organization.
     * @returns {boolean} 
     */
    getIsAutoSaveEnabled(): boolean;

    /**
     * Returns the LCID value that represents the base language for the organization.
     * @returns {number} 
     */
    getOrgLcid(): number;

    /**
     * Returns the unique text value of the organization’s name.
     * @returns {string} 
     */
    getOrgUniqueName(): string;

    /**
     * Returns a dictionary object of key value pairs that represent the query string arguments that were passed to the page.
     * @returns {any[]} 
     */
    getQueryStringParameters(): any[];

    /**
     * Returns the difference between the local time and Coordinated Universal Time (UTC).
     * @returns {number} 
     */
    getTimeZoneOffsetMinutes(): number;

    /**
     * Returns the GUID of the SystemUser.Id value for the current user.
     * @returns {string} 
     */
    getUserId(): string;

    /**
     * Returns the LCID value that represents the Microsoft Dynamics 365 Language Pack that the user selected as their preferred language.
     * @returns {number} 
     */
    getUserLcid(): number;

    /**
     * Returns the name of the current user.
     * @returns {string} 
     */
    getUserName(): string;

    /**
     * Returns an array of strings that represent the GUID values of each of the security roles that the user is associated with or any teams that the user is associated with.
     * @returns {Array<string>} 
     */
    getUserRoles(): Array<string>;

    /**
     * Prepends the organization name to the specified path.
     * @param sPath A local path to a resource.
     * @returns {string}
     */
    prependOrgName(sPath: string): string;
}

interface EventArgs {
    /**
     * Returns a value indicating how the save event was initiated by the user.
     * @returns {} 
     */
    getSaveMode(): saveModeType; 

    /**
     * Returns a value indicating whether the save event has been canceled because the preventDefault method was used in this event hander or a previous event handler.
     * @returns {boolean} 
     */
    isDefaultPrevented(): boolean;

    /**
     * Cancels the save operation, but all remaining handlers for the event will still be executed.
     */
    preventDefault(): void;

    /**
     * Returns a string that is either “next” or “previous” to show the direction of the stage change.
     * @returns {stageDirectionType}
     */
    getDirection(): stageDirectionType;

    /**
     * Returns a stage object. Except when the navigation moves to a new entity, the stage returned represents the destination stage object,that is, the next active stage. When the navigation moves to a new entity, the stage is the stage being navigated from, that is, the previous active stage object. More information: Stage methods.
     * @returns {ProcessStage} Returns a stage object. Except when the navigation moves to a new entity, the stage returned represents the destination stage object,that is, the next active stage.
     */
    getStage(): ProcessStage;
}

interface Data {
    /**
    * Asynchronously refreshes and optionally saves all the data of the form without reloading the page.
    * @param save {boolean} true if the data should be saved after it is refreshed, otherwise false.
    **/
    refresh(save?: boolean): RefreshResults;

    /**
     * saves the record asynchronously with the option to set callback functions to be executed after the save operation is completed.
     * With Microsoft Dynamics CRM Online 2015 Update 1 or later you can also set an object to control how appointment, recurring appointment, or service activity records are processed.
     * @param saveOptions {SaveOptions} With Microsoft Dynamics CRM Online 2015 Update 1 or later the object passed can indicate whether to use the Book or Reschedule messages rather than the Create or Update messages.
     * @returns {} 
     */
    save(saveOptions?: SaveOptions): SaveResults;

    entity: Entity;

    /**
     * The Xrm.Page.data.process namespace provides events, methods, and objects to interact with the business process flow data in a form.
     */
    process: ProcessData;
}

interface RefreshResults {
    /**
     * @param successCallback A function to call when the operation succeeds.
     * @param errorCallback A function to call when the operation fails.
     * @returns {} 
     */
    then(successCallback: Callback<string>, errorCallback: Callback<CodeMessage>): void;
}

interface SaveOptions {
    /**
    * With Microsoft Dynamics CRM Online 2015 Update 1 or later the object passed can indicate whether to use the Book or Reschedule messages rather than the Create or Update messages.
    * This option is only applicable when used with appointment, recurring appointment, or service activity records.
    * If you wish to apply these options, pass the following object as this parameter:
    **/
    UseSchedulingEngine: boolean;
}

interface SaveResults {
    /**
     * @param successCallback A function to call when the operation succeeds.
     * @param errorCallback A function to call when the operation fails.
     * @returns {} 
     */
    then(successCallback: Callback<string>, errorCallback: Callback<CodeMessage>): void;
}

interface UI {
    /**
     * Method to close the form.
     * @returns {} 
     */
    close(): void;

    /**
     * A collection of all the controls on the page.
     */
    controls: Collection<Control>;

    /**
     * Use the formSelector.getCurrentItem method to retrieve information about the form currently in use and the formSelector.items collection containing information about all the forms available for the user.
     */
    formSelector: FormSelector;

    /**
     * Method to get the form context for the record.
     * @returns {number} 
     */
    getFormType(): number;

    /**
     * 
     */
    navigation: Navigation;

    /**
     * Use this method to remove form level notifications.
     * @param uniqueId A unique identifier for the message used with setFormNotification to set the notification.
     * @returns {boolean} True if the method succeeded, otherwise false.
     */
    clearFormNotification(uniqueId: string): boolean;

    /**
     * Use this method to display form level notifications. You can display any number of notifications and they will be displayed until they are removed using clearFormNotification. The height of the notification area is limited so each new message will be added to the top. Users can scroll down to view older messages that have not yet been removed.
     * @param message The text of the message.
     * @param level The level defines how the message will be displayed. (ERROR, WARNING, INFO)
     * @param uniqueId A unique identifier for the message used with clearFormNotification to remove the notification.
     * @returns {} A unique identifier for the message used with clearFormNotification to remove the notification.
     */
    setFormNotification(message: string, level: string, uniqueId: string): boolean;

    /**
     * A collection of all the quick view controls on a form using the new form rendering engine (also called "turbo forms"). For information about the supported methods and properties for this collection and objects within the collection, see Xrm.Page.ui quickForms (client-side reference)
     */
    quickForms: Collection<QuickForm>;

    /**
     * Method to cause the ribbon to re-evaluate data that controls what is displayed in it.
     * @returns {} 
     */
    refreshRibbon(): void;

    /**
     * A collection of all the tabs on the page.
     */
    tabs: Collection<Tab>;

    /**
     * Method to get the height of the viewport in pixels.
     * @returns {number} 
     */
    getViewPortHeight(): number;

    /**
     * Method to get the width of the viewport in pixels.
     * @returns {number} 
     */
    getViewPortWidth(): number;

    /**
     * With Microsoft Dynamics 365 (online & on-premises), the Xrm.Page.ui.process namespace provides methods to interact with the business process flow control in a form.
     */
    process: ProcessUI;
}

interface Entity {
    /**
     * The collection of attributes for the entity.
     */
    attributes: Collection<Attribute>;

    /**
     * Returns a string representing the XML that will be sent to the server when the record is saved. Only data in fields that have changed are set to the server.
     * @returns {string} 
     */
    getDataXml(): string;

    /**
     * Returns a string representing the logical name of the entity for the record.
     * @returns {string} The name of the entity.
     */
    getEntityName(): string;

    /**
     * Returns a string representing the GUID id value for the record.
     * @returns {string} The GUID Id value for the record.
     */
    getId(): string;

    /**
     * Returns a Boolean value that indicates if any fields in the form have been modified.
     * @returns {boolean} 
     */
    getIsDirty(): boolean;

    /**
     * Adds a function to be called when the record is saved.
     * @param function function reference
     * @returns {} 
     */
    addOnSave(callBack: EventCallback): void;

    /**
     * Removes a function to be called when the record is saved.
     * @param callBack function reference
     * @returns {} 
     */
    removeOnSave(callBack: EventCallback): void;

    /**
     * Gets a string for the value of the primary attribute of the entity.
     * @returns {string} The value of the primary attribute of the entity.
     */
    getPrimaryAttributeValue(): string;

    /**
     * Saves the record synchronously with the options to close the form or open a new form after the save is completed.
     * @param argument
     * # null
     * # saveandclose
     * # saveandnew
     * @returns {} 
     */
    save(argument?: saveArguments): void;
}

interface ProcessUI {
    /**
     * Use this method to retrieve the display state for the business process control.
     * @returns {displayStateType} Returns “expanded” when the control is expanded, “collapsed” when the control is collapsed.
     */
    getDisplayState(): displayStateType;

    /**
     * Use this method to expand or collapse the business process flow control.
     * @param displayState {displayStateType} Use “expanded” to expand the control, “collapsed” to collapse it.
     */
    setDisplayState(displayState: displayStateType): void;

    /**
     * @returns {boolean} Returns true when the control is visible; otherwise, false.
     */
    getVisible(): boolean;

    /**
     * Use this method to show or hide the business process flow control.
     * @param visible {booelan} Use true to show the control; otherwise, false.
     */
    setVisible(visible: boolean): void;
}

interface ProcessData {
    /**
     * Returns a Process object representing the active process.
     * @returns {Process} See Process methods for the methods to access the properties of the process returned.
     */
    getActiveProcess(): Process;

    /**
     * Set a Process as the active process.
     * If there is an active instance of the process, the entity record is loaded with the instance ID. If there is no active instance of the current process, a new process instance is created and the entity record is loaded with the instance ID. If there are multiple instances of the current process, the record is loaded with first instance of the active process as per the defaulting logic, that is the most recently used process instance per user.
     * @param processId  {string} The Id of the process to make the active process.
     * @param callbackFunction A function to call when the operation is complete. This callback function is passed one of the following string values to indicate whether the operation succeeded.
     */
    setActiveProcess(processId: string, callbackFunction: Callback<ProcessActivationResultType>): void;

    /**
     * Returns all the process instances for the entity record that the calling user has access to.
     * @param callbackFunction Remarks: The callback function is passed an object with the following attributes and their corresponding values as the key: value pair.
     */
    getProcessInstances(callbackFunction: Callback<Collection<ProcessInstance>>): void;

    /**
     * Sets a process instance as the active instance.
     * @param processInstanceId The Id of the process instance to set as the active instance.
     * @param callbackFunction A function to call when the operation is complete. This callback function is passed one of the following string values to indicate whether the operation succeeded.
     */
    setActiveProcessInstance(processInstanceId: string, callbackFunction: Callback<ProcessActivationResultType>): void;

    /**
     * Returns a Stage object representing the active stage
     * @returns {ProcessStage} Remarks: See Stage methods for the methods to access the properties of the stage returned.
     */
    getActiveStage(): ProcessStage;

    /**
     * Set a completed stage as the active stage.
     * @param stageId Remarks: The ID of the completed stage for the entity to make the active stage.
     * @param callbackFunction Remarks: An optional function to call when the operation is complete.
     * @returns {} 
     */
    setActiveStage(stageId: string, callbackFunction?: Callback<StageActivationResultType>): void;

    /**
     * Use this method to get a collection of stages currently in the active path with methods to interact with the stages displayed in the business process flow control.
     * The active path represents stages currently rendered in the process control based on the branching rules and current data in the record.
     * @returns {Collection<ProcessStage>} Remarks: A collection of all completed stages, the currently active stage, and the predicted set of future stages based on satisfied conditions in the branching rule. This may be a subset of the stages returned with Xrm.Page.data.process.getActiveProcess because it will only include those stages which represent a valid transition from the current stage based on branching that has occurred in the process.
     */
    getActivePath(): Collection<ProcessStage>;

    /**
     * Use this method to asynchronously retrieve the enabled business process flows that the user can switch to for an entity.
     * @param callbackFunction Remarks: The callback function must accept a parameter that contains an object with dictionary properties where the name of the property is the Id of the business process flow and the value of the property is the name of the business process flow.
     * The enabled processes are filtered according to the user’s privileges. The list of enabled processes is the same ones a user can see in the UI if they want to change the process manually.
     * @returns {} 
     */
    getEnabledProcesses(callbackFunction: Callback<Array<Process>>): void;

    /**
     * Use this method to get the currently selected stage.
     * @returns {ProcessStage} The currently selected stage.
     */
    getSelectedStage(): ProcessStage;

    /**
     * Use this to add a function as an event handler for the OnStageChange event so that it will be called when the business process flow stage changes.
     * @param handler Remarks: The function will be added to the bottom of the event handler pipeline. The execution context is automatically set to be the first parameter passed to the event handler. See Execution context (client-side reference) for more information.
     * You should use a reference to a named function rather than an anonymous function if you may later want to remove the event handler.
     */
    addOnStageChange(handler: EventCallback): void;

    /**
     * Use this to remove a function as an event handler for the OnStageChange event.
     * @param handler Remarks: If an anonymous function is set using the addOnStageChange method it cannot be removed using this method.
     */
    removeOnStageChange(handler: EventCallback): void;

    /**
     * Use this to add a function as an event handler for the OnStageSelected event so that it will be called when a business process flow stage is selected.
     * @param handler Remarks: The function will be added to the bottom of the event handler pipeline. The execution context is automatically set to be the first parameter passed to the event handler. See Execution context (client-side reference) for more information.
     */
    addOnStageSelected(handler: EventCallback): void;

    /**
     * Use this to remove a function as an event handler for the OnStageSelected event.
     * @param handler 
     * @returns {} 
     */
    removeOnStageSelected(handler: EventCallback): void;

    /**
     * Use this to add a function as an event handler for the OnProcessStatusChange event event so that it will be called when the business process flow status changes.
     * @param handler Remarks: The function will be added to the bottom of the event handler pipeline. The execution context is automatically set to be the first parameter passed to the event handler. See Execution context (client-side reference) for more information.
     * You should use a reference to a named function rather than an anonymous function if you may later want to remove the event handler.
     * This method was introduced in December 2016 update for Dynamics 365 (online and on-premises).
     */
    addOnProcessStatusChange(handler: EventCallback): void;

    /**
     * Use this to remove a function as an event handler for the OnProcessStatusChange event event.
     * @param handler Remarks: If an anonymous function is set using the addOnProcessStatusChange method it cannot be removed using this method.
     * This method was introduced in December 2016 update for Dynamics 365 (online and on-premises).
     */
    removeOnProcessStatusChange(handler: EventCallback): void;

    /**
     * Progresses to the next stage.
     * @param callbackFunction Remarks: An optional function to call when the operation is complete. This callback function is passed one of the following string values to indicate whether the operation succeeded.
     * @returns {} 
     */
    moveNext(callbackFunction?: Callback<StageActivationResultType>): void;

    /**
     * Moves to the previous stage. You can use movePrevious to a previous stage in a different entity.
     * @param callback Function Remarks: An optional function to call when the operation is complete. This callback function is passed one of the following string values to indicate whether the operation succeeded.
     */
    movePrevious(callbackFunction?: Callback<StageActivationResultType>): void;
}

interface ProcessInstance {
    CreatedOn: string;
    ProcessDefinitionID: string;
    ProcessDefintionName: string;
    ProcessInstanceID: string;
    ProcessInstanceName: string;
    StatusCodeName: processInsanceType; 
}

interface Process {
    /**
     * Returns the unique identifier of the process
     * @returns {string} Value represents the string representation of a GUID value.
     */
    getId(): string;

    /**
     * Returns the name of the process
     * @returns {string} Returns the name of the process
     */
    getName(): string;

    /**
     * Returns an collection of stages in the process
     * @returns {Collection<ProcessStage>}
     */
    getStages(): Collection<ProcessStage>;

    /**
     * Returns true if the process is rendered, false if not
     * @returns {boolean} Remarks: If the form used has been upgraded from a previous version of Microsoft Dynamics 365 and has not been upgraded to use new forms, the business process flow control cannot be rendered. More information: TechNet: Update your forms to Microsoft Dynamics CRM 2013 or Microsoft Dynamics CRM Online Fall '13
     */
    isRendered(): boolean;

    /**
     * Returns the unique identifier of the process instance.
     * @returns {string} Remarks: Value represents the string representation of a GUID value.
     */
    getInstanceId(): string;

    /**
     * Returns the name of the process instance.
     * @returns {string} 
     */
    getInstanceName(): string;

    /**
     * Returns the current status of the process instance.
     * @returns {processInsanceType} Remarks: This method will return one of the following values: active, abandoned, or finish.
     */
    getStatus(): processInsanceType;

    /**
     * Sets the current status of the active process instance.
     * @param status Remarks: The values can be active, abandoned, or finish.
     * @param callbackFunction Remarks: An optional function to call when the operation is complete. This callback function is passed the new status as a string value.
     * @returns {} 
     */
    setStatus(status: processInsanceType, callbackFunction: Callback<ProcessActivationResultType>): void;
}

interface ProcessStage {
    /**
     * Returns an object with a getValue method which will return the integer value of the business process flow category.
     * @returns {number}
     * 0 - Qualify
     * 1 - Develop
     * 2 - Propose
     * 3 - Close
     * 4 - Identify
     * 5 - Research
     * 6 - Resolve
     */
    getCategory(): number;

    /**
     * Returns the logical name of the entity associated with the stage.
     * @returns {string} 
     */
    getEntityName(): string;

    /**
     * Returns the unique identifier of the stage
     * @returns {string} 
     */
    getId(): string;

    /**
     * Returns the name of the stage
     * @returns {string} 
     */
    getName(): string;

    /**
     * Returns the status of the stage
     * @returns {processStageStatusType} Remarks: This method will return either active or inactive.
     */
    getStatus(): processStageStatusType;

    /**
     * Returns a collection of steps in the stage.
     * @returns {Collection<ProcessStep>} See Step methods for methods to access the property values of the step.
     */
    getSteps(): CollectionArray<ProcessStep>;
}

interface ProcessStep {
    /**
     * Returns the logical name of the attribute associated to the step.
     * @returns {string} Remarks: Some steps don’t contain an attribute value.
     */
    getAttribute(): string;

    /**
     * Returns the name of the step
     * @returns {string} 
     */
    getName(): string;

    /**
     * Returns whether the step is required in the business process flow.
     * @returns {boolean} Remarks: Returns true if the step is marked as required in the Business Process Flow editor; otherwise, false. There is no connection between this value and the values you can change in the Xrm.Page.data.entity attribute RequiredLevel methods.
     */
    isRequired(): boolean;


}

interface Tab {
    /**
     * Returns a value that indicates whether the tab is collapsed or expanded.
     * 
     * @returns {string} Possible values:
     * # expanded
     * # collapsed
     */
    getDisplayState(): displayStateType;

    /**
     * Sets the tab to be collapsed or expanded.
     * @param value Valid argument values:
     * # expanded
     * # collapsed
     * @returns {} 
     */
    setDisplayState(value: displayStateType): void;

    /**
     * Returns the name of the tab.
     * @returns {string} 
     */
    getName(): string;

    /**
     * Returns the Xrm.Page.ui (client-side reference) object.
     * @returns {UI} 
     */
    getParent(): UI;

    /**
     * Returns the label for the tab.
     * @returns {string} 
     */
    getLabel(): string;

    /**
     * Sets the label for the tab.
     * @param value The label text to set.
     * @returns {} 
     */
    setLabel(value: string): void;

    /**
     * Sets the focus on the tab.
     * @returns {} 
     */
    setFocus(): void;

    /**
     * Returns true if the tab is visible, otherwise returns false.
     * @returns {boolean} 
     */
    getVisible(): boolean;

    /**
     * Sets a value to show or hide the tab.
     * @param value 
     * @returns {} 
     */
    setVisible(value: boolean): void;
}

interface Section {
    /**
     * The section controls collection provides access to the controls within a section. See Collections (client-side reference) for information about the methods exposed by collections.
     */
    controls: Collection<Control>;

    /**
     * Method to return the name of the section.
     * @returns {string} 
     */
    getName(): string;

    /**
     * Method to return the tab containing the section.
     * @returns {Tab} 
     */
    getParent(): Tab;

    /**
     * Returns the label for the section.
     * @returns {string} 
     */
    getLabel(): string;

    /**
     * Sets the label for the section.
     * @param value The label text to set.
     * @returns {} 
     */
    setLabel(value: string): void;

    /**
     * Returns true if the section is visible, otherwise returns false.
     * @returns {boolean} 
     */
    getVisible(): boolean;

    /**
     * Sets a value to show or hide the section.
     * @param value 
     * @returns {} 
     */
    setVisible(value: boolean): void;
}

interface QuickForm {
    /**
     * Gets the constituent control in a quick view control.
     * @param name The argument can either be the name or the index value of the constituent control in a quick view control. For example: quickViewControl.getControl(0) or quickViewControl.getControl("firstname")
     * @returns {Control} The constituent control instance in the quick view control.
     */
    getControl(name: string | number): Control;

    /**
     * Returns a string value that categorizes quick view controls.
     * @returns {string} For a quick view control, the method returns quickform.
     */
    getControlType(): string;

    /**
     * Returns the name assigned to the quick view control.
     * @returns {string} The name of the quick view control.
     */
    getName(): string;

    /**
     * Returns a reference to the section object that contains the control.
     * @returns {Section} Xrm.Page.ui section (client-side reference) object.
     */
    getParent(): Section;

    /**
     * Returns a value that indicates whether the quick view control is currently visible.
     * @returns {boolean} True if the quick view control is visible; otherwise, false.
     */
    getVisible(): boolean;

    /**
     * Returns the label for the quick view control.
     * @returns {string} 
     */
    getLabel(): string;

    /**
     * Sets the label for the quick view control.
     * @param value The label text to set.
     * @returns {} 
     */
    setLabel(value: string): void;

    /**
     * Returns whether the data binding for the constituent controls in a quick view control is complete.
     * @returns {boolean} True is the data binding for a constituent control is complete, otherwise false.
     */
    isLoaded(): boolean;

    /**
     * Refreshes the data displayed in a quick view control.
     * @returns {} 
     */
    refresh(): void;
}

interface Attribute {
    /**
     * Returns a value that represents the value set for an OptionSet or Boolean attribute when the form opened.
     * Attribute Types: Optionset and Boolean
     * @returns {number} The initial value for the attribute.
     */
    getInitialValue(): number;

    /**
     * Returns an option object with the value matching the argument passed to the method.
     * Attribute Types: optionset
     * @param value String or Number value
     * @returns {} 
     */
    getOption(value: number | string): Object;

    /**
     * Returns an array of option objects representing the valid options for an optionset attribute.
     * @returns {Array<Object>} Array of option objects
     */
    getOptions(): Array<Object>;

    /**
     * Returns the option object that is selected in an optionset attribute.
     * Attribute Types: optionset
     * @returns {Object} Option object with text and value properties.
     */
    getSelectedOption(): Object;

    /**
     * Returns a string value of the text for the currently selected option for an optionset attribute.
     * Attribute Types: optionset
     * @returns {string} The text value of the selected option.
     */
    getText(): string;

    /**
     * Access controls associated with attributes.
     */
    controls: Collection<Control>;

    /**
     * Returns a string value that represents the type of attribute.
     * Attribute Types: All
     * @returns {string} This method will return one of the following string values:
     *   # boolean
     *   # datetime
     *   # decimal
     *   # double
     *   # integer
     *   # lookup
     *   # memo
     *   # money
     *   # optionset
     *   # string
     */
    getAttributeType(): string;

    /**
     * Returns a string value that represents formatting options for the attribute.
     * Attribute Types: All
     * @returns {string} This method will return one of the following string values or null:
     *   # date
     *   # datetime
     *   # duration
     *   # email
     *   # language
     *   # none
     *   # phone
     *   # text
     *   # textarea
     *   # tickersymbol
     *   # timezone
     *   # url
     */
    getFormat(): string;

    /**
     * Returns a Boolean value indicating if there are unsaved changes to the attribute value.
     * Attribute Types: All
     * @returns {boolean} True if there are unsaved changes, otherwise false.
     */
    getIsDirty(): boolean;

    /**
     * Returns a Boolean value indicating whether the lookup represents a partylist lookup. Partylist lookups allow for multiple records to be set, such as the To: field for an email entity record.
     * Attribute Types: lookup
     * @returns {boolean} True if the lookup attribute is a partylist, otherwise false.
     */
    getIsPartyList(): boolean;

    /**
     * Returns a number indicating the maximum length of a string or memo attribute.
     * Attribute Types: string, memo
     * Note: The email form description attribute is a memo attribute, but it does not have a getMaxLength method.
     * @returns {number} The maximum allowed length of a string for this attribute.
     */
    getMaxLength(): number;

    /**
     * Returns a string representing the logical name of the attribute.
     * Attribute Types: All
     * @returns {string} The logical name of the attribute.
     */
    getName(): string;

    /**
     * Returns the Xrm.Page.data.entity object that is the parent to all attributes.
     * Attribute Types: All
     * @returns {Entity} Xrm.Page.data.entity object.
     */
    getParent(): Entity;

    /**
     * Returns an object with three Boolean properties corresponding to privileges indicating if the user can create, read or update data values for an attribute. This function is intended for use when Field Level Security modifies a user’s privileges for a particular attribute.
     * Attribute Types: All
     * @returns {UserPrivilege} The object has three Boolean properties:
     * # canRead
     * # canUpdate
     * # canCreate
     */
    getUserPrivilege(): UserPrivilege;

    /**
     * Returns a number indicating the maximum allowed value for an attribute.
     * Attribute Types: money, decimal, integer, double
     * @returns {number} The maximum allowed value for the attribute.
     */
    getMax(): number;

    /**
     * Returns a number indicating the minimum allowed value for an attribute.
     * Attribute Types: money, decimal, integer, double
     * @returns {number} The minimum allowed value for the attribute.
     */
    getMin(): number;

    /**
     * Returns the number of digits allowed to the right of the decimal point.
     * Attribute Types: money, decimal, double, and integer
     * @returns {number} The number of digits allowed to the right of the decimal point.
     */
    getPrecision(): number;

    /**
     * Sets a function to be called when the attribute value is changed.
     * @param callBack function pointer
     * @returns {} 
     */
    addOnChange(callBack: EventCallback): void;

    /**
     * Removes a function from the OnChange event hander for an attribute.
     * @param callBack function pointer
     * @returns {} 
     */
    removeOnChange(callBack: EventCallback): void;

    /**
     * Causes the OnChange event to occur on the attribute so that any script associated to that event can execute.
     * Attribute Types: All
     * @returns {} 
     */
    fireOnChange(): void;

    /**
     * Returns a string value indicating whether a value for the attribute is required or recommended.
     * Attribute Types: All
     * @returns {string} Returns one of the three possible values:
     * # none
     * # required
     * # recommended
     */
    getRequiredLevel(): RequiredLevel;

    /**
     * Sets whether data is required or recommended for the attribute before the record can be saved.
     * Attribute Types: All
     * @param requirementLevel One of the following values:
     * # none
     * # required
     * # recommended
     * @returns {} 
     */
    setRequiredLevel(requirementLevel: RequiredLevel): void;

    /**
     * Returns a string indicating when data from the attribute will be submitted when the record is saved.
     * Attribute Types: All
     * @returns {string} Returns one of the three possible values:
     * # always
     * # never
     * # dirty
     */
    getSubmitMode(): SubmitMode;

    /**
     * Sets whether data from the attribute will be submitted when the record is saved.
     * Attribute Types: All
     * @param submitMode One of the following values:
     * # always
     * # never
     * # dirty
     * @returns {}
     */
    setSubmitMode(submitMode: SubmitMode): void;

    /**
     * Retrieves the data value for an attribute.
     * Attribute Types: All
     * @returns {boolean|Date|number|Array<Lookup>|string} Depends on type of attribute
     */
    getValue(): boolean | Date | number | Array<Lookup> | string;

    /**
     * Sets the data value for an attribute.
     * Attribute Types: All
     * @param value Depends on the type of attribute.
     * @returns {} 
     */
    setValue(value: boolean | Date | number | Array<Lookup> | string): void;
}

interface EventCallback {
    (execContext?: ExecutionContext): void;
}

interface AttributeControlCallback<TAttributeControl> {
    (attribute: TAttributeControl, index: number): TAttributeControl;
}

interface FormSelector {
    getCurrentItem(): Object;
    items: Array<FormItem>;
}

interface FormItem {
    /**
     * Returns the GUID ID of the form.
     * @returns {String} Guid of the Form 
     */
    getId(): string;

    /**
     * Returns the label of the form.
     * @returns {} 
     */
    getLabel(): string;

    /**
     * Opens the specified form.
     * @returns {} 
     */
    navigate(): void;
}

interface Control {
    /**
     * Use this to show up to 10 matching strings in a drop-down list as users press keys to type character in a specific text field. You can also add a custom command with an icon at the bottom of the drop-down list. On selecting an item in the drop-down list, the value in the text field changes to the selected item, the drop-down list disappears, and the OnChange event for the text field is invoked.
     * @param object Object that defines the result set, which includes results and commands, to be displayed in the auto-completion drop-down list.
     * Call this method in a function that you added using the addOnKeyPress method to execute on the keypress event.
     * @returns {} 
     */
    showAutoComplete(object: Object);

    /**
     * Use this function to hide the auto-completion drop-down list you configured for a specific text field.
     * @returns {} 
     */
    hideAutoComplete(): void;

    /**
     * Returns whether the control is disabled.
     * @returns {boolean} 
     */
    getDisabled(): boolean;

    /**
     * Sets whether the control is disabled.
     * @param value 
     * @returns {} 
     */
    setDisabled(value: boolean): void;

    /**
     * Returns the attribute that the control is bound to.
     * @returns {Attribute} 
     */
    getAttribute(): Attribute;

    /**
     * Returns a value that categorizes controls.
     * @returns {string} 
     */
    getControlType(): ControlType;

    /**
     * Returns the name assigned to the control.
     * @returns {string} 
     */
    getName(): string;

    /**
     * Returns a reference to the section object that contains the control.
     * @returns {Section} 
     */
    getParent(): Section;

    /**
     * Gets the latest value in a control as the user types characters in a specific text or number field. This method helps you to build interactive experiences by validating data and alerting users as they type characters in a control.
     * @returns {string} 
     */
    getValue(): string;

    /**
     * Use this to add a function as an event handler for the keypress event so that the function is called when you type a character in the specific text or number field
     * @param Callback that should execute on Keystroke 
     * @returns {} 
     */
    addOnKeyPress(reference: Function): void;

    /**
     * Use this to remove an event handler for a text or number field that you added using addOnKeyPress.
     * @param reference function reference
     * @returns {} 
     */
    removeOnKeyPress(reference: Function): void;

    /**
     * Use this to manually fire an event handler that you created for a specific text or number field to be executed on the keypress event.
     * @returns {} 
     */
    fireOnKeyPress(): void;

    /**
     * Returns the label for the control.
     * @returns {string} 
     */
    getLabel(): string;

    /**
     * Sets the label for the control.
     * @param label The new label for the control.
     * @returns {} 
     */
    setLabel(label: string): void;

    /**
     * Use to add filters to the results displayed in the lookup. Each filter will be combined with any previously added filters as an “AND” condition.
     * @param filter The fetchXml filter element to apply.
     * @param entityLogicaName If this is set, the filter only applies to that entity type. Otherwise, it applies to all types of entities returned.
     * @returns {} 
     */
    addCustomFilter(filter: string, entityLogicaName?: string): void;

    /**
     * Adds a new view for the lookup dialog box.
     * @param viewId The string representation of a GUID for a view.
     * @param entityName The string representation of a GUID for a view.
     * @param viewDisplayName The name of the view.
     * @param fetchXml The fetchXml query for the view.
     * @param layoutXml The XML that defines the layout of the view.
     * @param isDefault Whether the view should be the default view.
     * @returns {} 
     */
    addCustomView(viewId: string, entityName: string, viewDisplayName: string, fetchXml: string, layoutXml: string, isDefault: boolean): void;

    /**
     * Returns the ID value of the default lookup dialog view.
     * @returns {string} 
     */
    getDefaultView(): string;

    /**
     * Sets the default view for the lookup control dialog box.
     * @param viewGuid The ID of the view to be set as the default view.
     * @returns {} 
     */
    setDefaultView(viewGuid: string): void;

    /**
     * Use this method to apply changes to lookups based on values current just as the user is about to view results for the lookup.
     * @param handler Function to add.
     * @returns {} 
     */
    addPreSearch(handler: Function): void;

    /**
     * Use this method to remove event handler functions that have previously been set for the PreSearch event.
     * @param handler Function to remove.
     * @returns {} 
     */
    removePreSearch(handler: Function): void;

    /**
     * Displays an error message for the control to indicate that data isn’t valid. When this method is used,  a red "X" icon appears next to the control. On Dynamics 365 mobile clients, tapping on the icon will display the message.
     * @param message The message to display
     * @param uniqueId The ID to use to clear just this message when using clearNotification.
     * @returns {boolean} Indicates whether the method succeeded
     */
    setNotification(message: string, uniqueId: string): boolean;

    /**
     * Displays an error or recommendation notification for a control, and lets you specify actions to execute based on the notification. When you specify an error type of notification, a red "X" icon appears next to the control. When you specify a recommendation type of notification, an "i" icon appears next to the control. On Dynamics 365 mobile clients, tapping on the icon will display the message, and let you perform the configured action by clicking the Apply button or dismiss the message.
     * @param object 
     * @returns {boolean} 
     */
    addNotification(object): boolean;

    /**
     * Remove a message already displayed for a control.
     * @param uniqueId The ID to use to clear a specific message set using setNotification or addNotification.
     * @returns {boolean} Indicates whether the method succeeded.
     */
    clearNotification(uniqueId: string): boolean;

    /**
     * Adds an option to an option set control.
     * @param option An option object to add to the OptionSet.
     * @param index (Optional) The index position to place the new option in. If not provided, the option will be added to the end.
     * @returns {} 
     */
    addOption(option: string, index?: number): void;

    /**
     * Clears all options from an option set control.
     * @returns {} 
     */
    clearOptions(): void;

    /**
     * Removes an option from an option set control.
     * @param number The value of the option you want to remove.
     * @returns {} 
     */
    removeOption(number: number): void;

    /**
     * Sets the focus on the control.
     * @returns {} 
     */
    setFocus(): void;

    /**
     * Get whether a date control shows the time portion of the date.
     * @returns {boolean} 
     */
    getShowTime(): boolean;

    /**
     * Specify whether a date control should show the time portion of the date.
     * @param bool 
     * @returns {boolean} 
     */
    setShowTime(bool: boolean): void;

    /**
     * Refreshes the data displayed in a subgrid.
     * @returns {} 
     */
    refresh(): void;

    /**
     * Returns a value that indicates whether the control is currently visible.
     * @returns {boolean} True if the control is visible; otherwise, false.
     */
    getVisible(): boolean;

    /**
     * Sets a value that indicates whether the control is visible
     * @param bool True if the control should be visible; otherwise, false.
     * @returns {} 
     */
    setVisible(bool: boolean): void;

    /**
     * Returns the value of the data query string parameter passed to a Silverlight web resource.
     * @returns {string} The data value passed to the Silverlight web resource.
     */
    getData(): string;

    /**
     * Sets the value of the data query string parameter passed to a Silverlight web resource.
     * @param data The data value to pass to the Silverlight web resource.
     * @returns {} 
     */
    setData(data: string): void;

    /**
     * Returns the default URL that an IFRAME control is configured to display. This method is not available for web resources.
     * @returns {string} The initial URL.
     */
    getInitialUrl(): string;

    /**
     * Returns the object in the form that represents an I-frame or web resource.
     * @returns {Object} Which object depends on the type of control.
     */
    getObject(): Object;

    /**
     * Returns the current URL being displayed in an IFRAME or web resource.
     * @returns {string} A URL representing the src property of the IFRAME or web resource.
     */
    getSrc(): string;

    /**
     * Sets the URL to be displayed in an IFRAME or web resource.
     * @param value The URL.
     * @returns {} 
     */
    setSrc(value: string): void;


    /**
    * If the subgrid control is not configured to display the view selector, calling this method on the ViewSelector returned by the GridControl.getViewSelector will throw an error.
    * @returns {Grid}
    **/
    getGrid(): Grid | null;


    getViewSelector(): ViewSelector;
}

interface Grid {
    /**
    * Returns a collection of every GridRow in the Grid.
    * @returns {Collection<GridRow>} Collection;
    **/
    getRows(): Collection<GridRow>;

    /**
    * Returns a collection of every selected GridRow in the Grid.
    * @returns {Collection<GridRow>} Collection;
    **/
    getSelectedRows(): Collection<GridRow>;

    /**
    * In the web application or the Dynamics 365 for Outlook client while connected to the server, this method returns the total number of records that match the filter criteria of the view, not limited by the number visible in a single page.
    * When the Dynamics 365 for Outlook client isn’t connected to the server, this number is limited to those records that the user has selected to take offline.
    * For Microsoft Dynamics 365 for tablets and Microsoft Dynamics 365 for phones this method will return the number of records in the subgrid.
    * @returns {number} Number of Rows;
    **/
    getTotalRecordCount(): number;
}

interface GridRow {
    /**
    * Returns the GridRowData for the GridRow.
    * @returns {GridRowData}
    **/
    getData(): GridRowData;
}

interface GridRowData {
    /**
    * Returns the GridEntity for the GridRowData
    * @returns {GridEntity}
    **/
    getEntity(): GridEntity;
}

interface GridEntity {
    /**
    * Returns the logical name for the record in the row.
    * @returns {string} the name of the Entity
    **/
    getEntityName(): string;

    /**
    *
    * @returns {Lookup}
    **/
    getEntityReference(): Lookup;

    /**
    * Returns the id for the record in the row.
    * @returns {string}
    **/
    getId(): string;

    /**
    * Returns the primary attribute value for the record in the row.
    * @returns {string} 
    **/
    getPrimaryAttributeValue(): string;
}

interface ViewSelector {
    /**
    * Use this method to get a reference to the current view.
    * @returns {Lookup}
    **/
    getCurrentView(): Lookup;

    /**
    * Use this method to determine whether the view selector is visible
    * @returns {boolean}
    **/
    isVisible(): boolean;

    /**
    * Use this method to set the current view.
    **/
    setCurrentView(view: Lookup)
}

interface Lookup {
    /**
     * the name of the entity displayed in the lookup
     */
    entityType: string;

    /**
     * The string representation of the GUID value for the record displayed in the lookup.
     */
    id: string;

    /**
     * The text representing the record to be displayed in the lookup
     */
    name: string;
}

interface Navigation {
    item: Collection<NavigationItem>;
}

interface Collection<T> {
    forEach(argument: CollectionCallback<T>): void; // Applies the action contained within a delegate function.    
    get(): CollectionArray<T>; // Returns one or more controls depending on the arguments passed. 
    get(argument: string): T; // Returns The control where the name matches the argument
    get(argument: number): T; // Returns The control where the index matches the number
    get(argument: CollectionCallback<T>): CollectionArray<T>; // Return Value The tab where the index matches the number
    getLength(): number; // Returns the number of controls in the collection
}

interface CollectionArray<T> {
    [index: number]: NavigationItem;
    [index: string]: NavigationItem;
    (index: number): NavigationItem;
    (index: string): NavigationItem;
}

interface NoneParamCallback {
    (): void;
}

interface CollectionCallback<T> {
    (item: T, index: number): T;
}

interface UserPrivilege {
    canRead: boolean;
    canUpdate: boolean;
    canCreate: boolean;
}

interface NavigationItem {
    /**
     * Returns the name of the item.
     * @returns {string} 
     */
    getId(): string;

    /**
     * Returns the label for the item.
     * @returns {string} 
     */
    getLabel(): string;

    /**
     * Sets the label for the item.
     * @param value The new label for the item.
     * @returns {} 
     */
    setLabel(value: string): void;

    /**
     * Sets the focus on the item.
     * @returns {} 
     */
    setFocus(): void;

    /**
     * Returns a value that indicates whether the item is currently visible.
     * @returns {boolean} 
     */
    getVisible(): boolean;

    /**
     * Sets a value that indicates whether the item is visible.
     * @param value Whether the item is visible.
     * @returns {} 
     */
    setVisible(value: boolean): void;
}

type saveModeType = 1 | 2 | 59 | 70 | 58 | 5 | 6 | 47 | 7 | 16 | 15;
type saveArguments = null | "saveandclose" | "saveandnew";
type displayStateType = "expanded" | "collapsed";
type RequiredLevel = "none" | "required" | "recommended";
type SubmitMode = "always" | "never" | "dirty";
type ControlType = "standard" | "iframe" | "lookup" | "optionset" | "subgrid" | "webresource" | "notes" | "timecontrol" | "kbsearch";
type processInsanceType = "active" | "abandoned" | "finish";
type processStageStatusType = "active" | "inactive";
type ProcessActivationResultType = "success" | "invalid";
type StageActivationResultType = "success" | "crossEntity" | "invalid" | "unreachable" | "dirtyForm";
type stageDirectionType = "next" | "previous";